package com.huike.web.controller.system;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.Page;
import com.huike.clues.domain.SysDict;
import com.huike.clues.mapper.SysDictMapper;
import com.huike.clues.service.impl.SysDictServiceImpl;
import com.huike.common.constant.ScheduleConstants;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictTypeDTO;
import com.huike.common.core.domain.entity.SysDictTypePageQueryDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.result.PageResult;
import com.huike.common.result.Result;
import com.huike.common.utils.DictUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @Desciption 数据字典类型信息相关接口
 * @Author LUXIN
 * @Date 2023-10-15
 */
@RestController
@RequestMapping("/system/dict")
@Api(tags = "数据字典类型信息相关接口")
@Slf4j
public class SysDictTypeController extends BaseController {
    @Resource
    private SysDictServiceImpl sysDictService;

    private SysDictMapper sysDictMapper;

    /**
     * 新增字典类型接口
     *
     * @param dict
     * @return
     */
    @PostMapping("/type")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("新增字典类型接口")
    public Result addType(@RequestBody SysDict dict) {
        log.info("开始执行新增字典类型接口，{}", dict);
        // 调用业务实现字典类型新增
        sysDictService.addType(dict);
        return Result.success();
    }

    /**
     * 分页查询数据字典类型接口
     *
     * @param sysDict
     * @return
     */
    @GetMapping("/type/list")
    @ApiOperation("分页查询数据字典类型接口")
    public TableDataInfo<List<SysDictTypeDTO>> typeListPage(SysDictTypeDTO sysDict) {
        log.info("开始执行分页查询数据字典类型接口，{}", sysDict);
        // 调用业务分页
        startPage();
        List<SysDictTypeDTO> list = sysDictService.PageList(sysDict);
        return getDataTable(list);
    }

    /**
     * 修改字典类型接口
     *
     * @param dict
     * @return
     */
    @PutMapping("/type")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改字典类型接口")
    public Result updateType(@RequestBody SysDict dict) {
        log.info("开始执行修改字典类型接口，{}", dict);
        // 调用业务开始实现修改字典类型方法
        sysDictService.updateType(dict);
        return Result.success();
    }

    /**
     * 清理数据字典缓存接口
     *
     * @return
     */
    @DeleteMapping("/type/clearCache")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("清理字典缓存接口")
    public Result clearCache() {
        log.info("开始执行清理字典缓存接口...");
        DictUtils.clearDictCache();
        return Result.success();
    }

    /**
     * 查询字典类型详细接口
     *
     * @param dictId
     * @return
     */
    @GetMapping("/type/{dictId}")
    @ApiOperation("查询字典类型详细接口")
    public Result<SysDictTypeDTO> queryDetails(@PathVariable Integer dictId) {
        log.info("开始执行查询字典类型详细接口，{}", dictId);
        // 调用业务层实现查询方法
        SysDictTypeDTO sysDictTypeDTO = sysDictService.findDictType(dictId);
        // 返回数据
        return Result.success(sysDictTypeDTO);
    }

    /**
     * 删除字典类型接口
     *
     * @param dictIds
     * @return
     */
    @DeleteMapping("/type/{dictIds}")
    @ApiOperation("删除字典类型接口")
    public Result deleteType(@PathVariable List<String> dictIds) {
        log.info("开始执行删除字典类型接口，{}", dictIds);
        // 调用业务层实现删除方法
        sysDictService.delete(dictIds);
        return Result.success();
    }

    /**
     * 获取字典选择框列表
     *
     * @return
     */
    @GetMapping("/type/optionselect")
    @ApiOperation("获取字典选择框列表")
    public Result<List<SysDict>> optionselect() {
        log.info("开始执行获取字典选择框列表...");
        SysDict sysDict = new SysDict();
        List<SysDict> statusList = sysDictService.findOptionselect(sysDict.getStatus());
        return Result.success(statusList);
    }

    /**
     * 导出数据
     */
    @GetMapping("/type/export")
    @ApiOperation("导出数据")
    public AjaxResult export(HttpServletResponse response) {
        log.info("执行导出数据");

        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/sys_dict_type.xlsx");
             ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(inputStream).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();

            List<SysDictTypeDTO> list = sysDictMapper.list();

            excelWriter.fill(list, writeSheet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }

}
